package solutions.leetcode.easy

/**
 * @author lizhidong
 * <a href="https://leetcode.cn/problems/check-if-two-string-arrays-are-equivalent/">1662. 检查两个字符串数组是否相等</a>
 */
private class Solution1662 {
    fun arrayStringsAreEqual(word1: Array<String>, word2: Array<String>): Boolean {
        var p1x = 0
        var p1y = 0
        var p2x = 0
        var p2y = 0
        while (p1x < word1.size && p2x < word2.size) {
            val w1 = word1[p1x]
            val w2 = word2[p2x]
            while (p1y < w1.length && p2y < w2.length) {
                if (w1[p1y] == w2[p2y]) {
                    p1y++
                    p2y++
                    var hasBreak = false
                    if (p1y >= w1.length) {
                        p1y = 0
                        p1x++
                        hasBreak = true
                    }
                    if (p2y >= w2.length) {
                        p2y = 0
                        p2x++
                        hasBreak = true
                    }
                    if (hasBreak) break
                } else {
                    return false
                }
            }
        }
        return p1x >= word1.size && p2x >= word2.size
    }
}

fun main() {
    println(Solution1662().arrayStringsAreEqual(
        arrayOf(
            "ab", "c"
        ),
        arrayOf(
            "a", "bc"
        )
    ))
}